package com.OrderingServer.Controller.buyer;

import com.OrderingServer.pojo.ProductCategory;
import com.OrderingServer.pojo.ProductInfo;
import com.OrderingServer.service.ProductCategoryService;
import com.OrderingServer.service.ProductInfoService;
import com.OrderingServer.vo.ProductInfoVO;
import com.OrderingServer.vo.ProductVO;
import com.OrderingServer.vo.ResultVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/sell/buyer")
public class BuyerProductController {
    @Autowired
    ProductCategoryService productCategoryService;
    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/product/list")
    public ResultVO list() {
        List<ProductVO> productVOList= (List<ProductVO>) this.redisTemplate.opsForValue().get("productVOList");
       if(productVOList==null){
           productVOList = new ArrayList<>();
           List<ProductInfo> productInfoList = productInfoService.list(new QueryWrapper<ProductInfo>().eq("del", 0));
           Set<Integer> categoryid = new HashSet<Integer>();
           for (ProductInfo productInfo : productInfoList) {
               categoryid.add(productInfo.getCategoryType());
           }
           List<ProductCategory> productCategoryList = productCategoryService.list(new QueryWrapper<ProductCategory>().eq("del", 0).in("category_id", categoryid));
           for (ProductCategory productCategory : productCategoryList) {
               ProductVO productVO = new ProductVO();
               productVO.setCategoryName(productCategory.getCategoryName());
               productVO.setCategoryType(productCategory.getCategoryId());
               List<ProductInfoVO> productInfoVOList = new ArrayList<ProductInfoVO>();
               for (ProductInfo productInfo : productInfoList) {
                   if (productInfo.getCategoryType().equals(productCategory.getCategoryId())) {
                       ProductInfoVO productInfoVO = new ProductInfoVO();
                       productInfoVO.setProductId(productInfo.getProductId());
                       productInfoVO.setProductName(productInfo.getProductName());
                       productInfoVO.setProductPrice(productInfo.getProductPrice());
                       productInfoVO.setProductDescription(productInfo.getProductDescription());
                       productInfoVO.setProductIcon(productInfo.getProductIcon());
                       productInfoVOList.add(productInfoVO);
                   }
               }
               productVO.setProductInfoVOList(productInfoVOList);
               productVOList.add(productVO);
           }
           redisTemplate.opsForValue().set("productVOList", productVOList);
           log.info("第一次查询，缓存中没有菜品数据，查询的是sql数据库，同时加入缓存！");

       }
       else{
           log.info("后续查询，缓存中有菜品数据，不查询sql数据库，因此此时有缓存");
       }
       return new ResultVO(0,"成功",productVOList);
    }
}

